package asteroids.expressions;

import java.util.*;

import asteroids.Ship;
import asteroids.types.*;

public class LessThanOrEqualExpression extends ComparisonExpression {

	public LessThanOrEqualExpression(int line, int column, Expression e1, Expression e2) {
		super(line, column, e1, e2);
	}

	@Override
	public Type evaluate(Ship ship, Map<String, Type> globals) {
		if(ship != null)
			return new BooleanType(((DoubleType) getFirstExpression().evaluate(ship, globals)).getValue() <= ((DoubleType) getSecondExpression().evaluate(ship, globals)).getValue());
		else
			return new BooleanType();
	}
	
	@Override
	public List<String> getTypeCheckErrorsExpressions(List<String> typeErrors, Map<String, Type> globals) {
		if(getFirstExpression().evaluate(null, globals).getClass() != DoubleType.class) 
			typeErrors.add("Line: " + getLine() + " Column: " + getColumn() + " - The left operand, " + getFirstExpression().toString() + " is expected to be from the type 'double'.");
		if(getSecondExpression().evaluate(null, globals).getClass() != DoubleType.class) 
			typeErrors.add("Line: " + getLine() + " Column: " + getColumn() + " - The right operand, " + getSecondExpression().toString() + " is expected to be from the type 'double'.");
		typeErrors = getFirstExpression().getTypeCheckErrorsExpressions(typeErrors, globals);
		typeErrors = getSecondExpression().getTypeCheckErrorsExpressions(typeErrors, globals);
		return typeErrors;
	}
	
	@Override
	public String toString() {
		return getFirstExpression().toString() + " <= " + getSecondExpression().toString();
	}

}
